﻿using UnityEngine;

public class MoveController : ActionController
{
    private int frame_accelerate = 5;    //无摩擦时，地面上加速需要的固定帧帧数 
    private int frame_slowDown = 5;      //无摩擦时，地面上减速需要的固定帧帧数

    private void FixedUpdate()
    {
        Move();
    }

    public void Move()
    {
        if (player.b_isDashing)
            return;

        float vx = player.VX;
        float vy = player.VY;
        int accelarateDirection; //0表示无输入，1表示水平输入与当前移动方向一致，-1表示相反
        if (player.drct_move_input == 0)
            accelarateDirection = 0;
        else if (player.drct_move_input * player.SgnX > 0)
            accelarateDirection = 1;
        else
            accelarateDirection = -1;
        vx += accelarateDirection * player.MaxHorizontalSpeed / frame_accelerate;

        if (player.InWindArea)
            return;

        if (player.Grounded)
        {
            //如果不希望加速，地面上会自然减速
            if (accelarateDirection <= 0)
            {
                vx -= player.MaxHorizontalSpeed / frame_slowDown;
                if (vx < 0) vx = 0;
            }
        }
        else
        {
            if (player.SgnY < 0 && vy > player.MaxFallSpeed)
                vy = player.MaxFallSpeed;
        }

        if (vx > player.MaxHorizontalSpeed) 
            vx = player.MaxHorizontalSpeed;

        m_rigidbody.velocity = new Vector2(player.SgnX * vx, player.SgnY * vy);
    }

}
